package com.shenmazong.servereduresume.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shenmazong.servereduresume.dto.TbEducation;
import com.shenmazong.servereduresume.dto.TbInformation;
import com.shenmazong.servereduresume.dto.TbLevel;
import com.shenmazong.servereduresume.dto.TbPractice;
import com.shenmazong.servereduresume.mapper.*;
import com.shenmazong.servereduresume.service.TbInformationService;
import com.shenmazong.servereduresume.service.TbPracticeService;
import com.shenmazong.servereduresume.service.TbProjectService;
import com.shenmazong.servereduresume.service.TbSchoolyardService;
import com.shenmazong.utils.ResultResponse;
import com.shenmazong.vo.BatchChooseIdVo;
import com.shenmazong.vo.ListParamVo;
import fr.opensagres.xdocreport.core.XDocReportException;
import fr.opensagres.xdocreport.document.IXDocReport;
import fr.opensagres.xdocreport.document.registry.XDocReportRegistry;
import fr.opensagres.xdocreport.template.IContext;
import fr.opensagres.xdocreport.template.TemplateEngineKind;
import fr.opensagres.xdocreport.template.formatter.FieldsMetadata;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 */
@Service
public class TbInformationServiceImpl extends ServiceImpl<TbInformationMapper, TbInformation>
implements TbInformationService {

    @Autowired //简历信息表
    private TbInformationMapper tbInformationMapper;

    @Autowired  //教育背景表
    private TbEducationMapper tbEducationMapper;

    @Autowired  //学历等级表
    private TbLevelMapper tbLevelMapper;

    @Autowired  //实习经验表
    private TbPracticeMapper tbPracticeMapper;

    @Autowired  //项目经历表
    private TbProjectMapper tbProjectMapper;

    @Autowired  //校园经历表
    private TbSchoolyardMapper tbSchoolyardMapper;

    @Override
    public ResultResponse findByUserIdInformationId(BatchChooseIdVo batchChooseIdVo) {
        Object o = batchChooseIdVo.getParamMap().get("userId");
        if (o == null){
            return ResultResponse.FAILED("非法参数");
        }
        List<TbInformation> list = null;
        try {
            String userId = String.valueOf(o);
            list = tbInformationMapper.selectList(new QueryWrapper<TbInformation>().lambda().eq(TbInformation::getUserId, userId));
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.FAILED();
        }
        return ResultResponse.SUCCESS(list);
    }

    @Override
    public ResultResponse saveInformation(TbInformation tbInformation) {
        try {
            tbInformationMapper.insert(tbInformation);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.FAILED();
        }
        return ResultResponse.SUCCESS();
    }

    @Override
    public ResultResponse updateInformation(TbInformation tbInformation) {
        try {
            tbInformationMapper.updateById(tbInformation);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.FAILED();
        }
        return ResultResponse.SUCCESS();
    }

    @Override
    public ResultResponse listPageInformation(ListParamVo listParamVo) {
        Page<TbInformation> page = new Page<>(listParamVo.getCurrent(), listParamVo.getSize());


        IPage<TbInformation> tbInformationIPage = null;
        try {

            tbInformationIPage = tbInformationMapper.listPageInformation(page,listParamVo.getParamMap());


        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.FAILED();
        }
        return ResultResponse.SUCCESS(tbInformationIPage);
    }

    @Override
    public ResultResponse deleteInformation(BatchChooseIdVo batchChooseIdVo) {
        if (batchChooseIdVo.getIds().size() <= 0){
            return ResultResponse.FAILED("至少选择一个删除");
        }
        try {
            for (Long id : batchChooseIdVo.getIds()) {
                UpdateWrapper<TbInformation> updateWrapper = new UpdateWrapper<TbInformation>().eq("information_id", id);
                updateWrapper.set("delete_flag",1);
                tbInformationMapper.delete(updateWrapper);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.FAILED();
        }
        return ResultResponse.SUCCESS();
    }



//    public void returnMap(Long id){
//
//    }

    @Override
    public ResultResponse downloadResume(Long id) throws XDocReportException, IOException {
    //获取Word模板，模板存放路径在项目的resources目录下
        InputStream ins = this.getClass().getResourceAsStream("src/main/resources/templates/ResumeModel.docx");
        //注册xdocreport实例并加载FreeMarker模板引擎
        IXDocReport report = XDocReportRegistry.getRegistry().loadReport(ins, TemplateEngineKind.Freemarker);
        IContext context = report.createContext();

//        if(context == null || report == null){
//            return ResultResponse.FAILED("未能找到模板");
//        }

        //创建要替换的文本变量
        //简历信息表
        TbInformation information = tbInformationMapper.selectById(id);
        //姓名
        context.put("Name", information.getInformationName());
        //出生日期
        context.put("Birthday", information.getInformationBirthday());
        //联系电话
        context.put("Tel", information.getInformationTel());
        //联系邮箱
        context.put("Email", information.getInformationEmail());
        //工作年限
        context.put("WorkNum", information.getWorkNum());
        //个人照片
        context.put("Img", information.getInformationImg());
        //性别1男0女2保密
        String sex = null;
        if (information.getInformationSex() == 1){
            sex = "男";
        }else if (information.getInformationSex() == 0){
            sex = "女";
        }else if (information.getInformationSex() == 2){
            sex = "保密";
        }
        context.put("Sex", sex);

        //身高cm
        context.put("Height", information.getInformationHeight());
        //体重kg
        context.put("Weight", information.getInformationWeight());
        //民族
        context.put("Nation", information.getInformationNation());
        //籍贯
        context.put("Place", information.getInformationPlace());
        //政治面貌0无1中共党员2中共预备党员3共青团员4普通群众5群众
        String politics = null;
        if (information.getPoliticsStatus() == 0){
            politics = "无";
        }else if (information.getPoliticsStatus() == 1){
            politics = "中共党员";
        }else if (information.getPoliticsStatus() == 2){
            politics = "中共预备党员";
        }else if (information.getPoliticsStatus() == 3){
            politics = "共青团员";
        }else if (information.getPoliticsStatus() == 4){
            politics = "普通群众";
        }else if (information.getPoliticsStatus() == 5){
            politics = "群众";
        }
        context.put("Politics", politics);

        //婚姻状况0无1已婚2未婚3离异4已婚已育
        String marriage = null;
        if (information.getInformationMarriage() == 0){
            marriage = "无";
        }else if (information.getInformationSex() == 1){
            marriage = "已婚";
        }else if (information.getInformationSex() == 2){
            marriage = "未婚";
        }else if (information.getInformationSex() == 3){
            marriage = "离异";
        }else if (information.getInformationSex() == 4){
            marriage = "已婚已育";
        }
        context.put("Marriage", marriage);

        //求职意向
        context.put("Intention", information.getInformationIntention());
        //意向城市
        context.put("City", information.getInformationCity());
        //期望薪资
        context.put("Salary", information.getExpectSalary());

        //入职时间0无1随时到岗2一周内到岗3一月内到岗4到岗时间另行商议
        String entry = null;
        if (information.getInformationEntry() == 0){
            entry = "无";
        }else if (information.getInformationSex() == 1){
            entry = "随时到岗";
        }else if (information.getInformationSex() == 2){
            entry = "一周内到岗";
        }else if (information.getInformationSex() == 3){
            entry = "一月内到岗";
        }else if (information.getInformationSex() == 4){
            entry = "到岗时间另行商议";
        }
        context.put("Entry", entry);

        //技能特长内容
        context.put("SkillText", information.getSkillText());
        //荣誉证书内容
        context.put("CertificateText", information.getCertificateText());
        // 自我评价
        context.put("SelfEvaluation", information.getSelfEvaluation());


        //教育背景表
        List<TbEducation> educationList = tbEducationMapper.selectList(new QueryWrapper<TbEducation>().lambda().eq(TbEducation::getInformationId, information.getInformationId()));
        List<TbLevel> levelList = null;
        if (educationList.size() > 0){
            context.put("educationList",educationList);
            FieldsMetadata fm = report.createFieldsMetadata();
            fm.load("educationList",TbEducation.class,true);
            //根据查到的教育背景 去查询对应的学历等级
            for (TbEducation education : educationList) {
                TbLevel tbLevel = tbLevelMapper.selectById(education.getLevelId());
                levelList.add(tbLevel);
            }
        }

        if (levelList != null){
            context.put("levelList",levelList);
            FieldsMetadata fm = report.createFieldsMetadata();
            fm.load("levelList",TbLevel.class,true);
        }

//        List<Goods> goodsList = new ArrayList<Goods>();
//        context.put("goods", goodsList);
//
//        //创建字段元数据
//        FieldsMetadata fm = report.createFieldsMetadata();
//        //Word模板中的表格数据对应的集合类型
//        fm.load("goods", Goods.class, true);
//
        //输出到本地目录
        FileOutputStream out = new FileOutputStream(new File("C:\\Users\\86136\\Desktop\\线上教育\\成品简历.docx"));
        report.process(context, out);

        return ResultResponse.SUCCESS();
    }
}




